package cn.itsource.user.service.impl;

import cn.itsource.base.exception.BusinessException;
import cn.itsource.base.service.impl.BaseServiceImpl;
import cn.itsource.user.domain.LoginInfo;
import cn.itsource.user.domain.User;
import cn.itsource.user.mapper.LoginInfoMapper;
import cn.itsource.user.service.ILoginInfoService;
import cn.itsource.util.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class LoginInfoServiceImpl extends BaseServiceImpl<LoginInfo> implements ILoginInfoService {

    @Autowired
    private LoginInfoMapper loginInfoMapper;
    // user 只是去接收 账户和密码
    @Override
    public LoginInfo login(User user) {
        LoginInfo loginInfo =loginInfoMapper.loadByCount(user.getPhone());
        //1.非空判断
        if(loginInfo==null){
            throw new BusinessException("用户未注册");
        }
        //2.验证密码是否正确
        //把用户输入的明文进行加盐 加密
        String byMd5 = MD5Utils.encrypByMd5(user.getPassword() + loginInfo.getSalt());
        if(!byMd5.equals(loginInfo.getPassword())){
            throw new BusinessException("账户或密码错误");
        }
        return loginInfo;
    }
}
